<?php
class Main_Controller_AdminAction extends Main_Controller_Action
{
	protected $_noAuthenticate = array(
		'module' => 'admin',
		'controller' => 'login',
		'action' => 'index'
	);
    
    /**
     * (non-PHPdoc)
     * @see library/Zend/Controller/Zend_Controller_Action#init()
     */
    public function init() 
    {
        $this->_setLayout();
		$this->_authenticate();	
		
		if ($isAuth = Zend_Auth::getInstance()->hasIdentity()) {
			if (! defined ("IS_NAVIGATION_LOADED")) {
				define("IS_NAVIGATION_LOADED", true);
	            $this->_helper->actionStack('index', 'navigation', 'admin');
			} 
		}	
    }
    
	/**
	 * @return void
	 */
    protected function _authenticate() 
    {
    	$moduleName = $this->_request->getModuleName();
    	$controllerName = $this->_request->getControllerName();
    	$actionName = $this->_request->getActionName();
    	
    	$isLoginRequest = ($moduleName == $this->_noAuthenticate['module']) 
    		&& ($controllerName == $this->_noAuthenticate['controller']) 
    		&& ($actionName == $this->_noAuthenticate['action']);
    	
    	$isAuth = Zend_Auth::getInstance()->hasIdentity(); 	
    		
    	if (!$isAuth && !$isLoginRequest) { 
    		$this->_helper->redirector->gotoSimple($this->_noAuthenticate['action'],
    			$this->_noAuthenticate['controller'], $this->_noAuthenticate['module']);
    	}
    	    	
    }
    
	/**
     * @return void
     */
    protected function _setLayout()
    {    
        $moduleName = $this->_request->getParam('module');
    	$controllerName = $this->_request->getParam('controller');
    	
    	if ($controllerName == 'login') {
    		$layout = 'login.layout';
    	} else if ($controllerName == 'install') {
    		$layout = 'install.layout';
    	} else {
    		$layout = 'admin.layout';
    	}

    	Zend_Layout::getMvcInstance()->setLayout($layout);
    }
}
